function sensorLevelClassification(subject, toilim, class1, class2, condition, lambda)
    % Classification on the ERF
	% Uses data in subject.subjectDir/subject.subjID_data.mat
	% subject: subject specific struct which should at least include the fields subjectID, subjectDir and MEGChannels (all MEG channels but the rejected ones)
	% toilim: toi interval [beginOfToi endOfToi]
	% class 1 and class 2: names of the classes on which classification should be performed
	% Note that triggercodes are at the moment still hardcoded.
    
    % requires ~7.8 minutes and 4.6 GB (for faces against tools)
    display(condition)
  %  class(condition)
   eval(['load ', subject.subjectDir, '/', subject.subjID, '_', condition, '_data.mat data;']);

    cfg = [];
    cfg.toilim = toilim;
    data_segmented = ft_redefinetrial(cfg, data);

    % sort trialtypes (not before segmentation, as trials may get lost)
    [faceTrl sceneTrl bodyTrl toolTrl wordTrl] = deal([]);

    if strcmp(subject.subjID, 'P01')
        for i = 1 : length(data_segmented.trialinfo)
            if data_segmented.trialinfo(i) == 201 %faces
                faceTrl = [faceTrl i];
            elseif data_segmented.trialinfo(i) == 202 %scenes
                sceneTrl = [sceneTrl i];
            elseif data_segmented.trialinfo(i) == 203 %bodies
                bodyTrl = [bodyTrl i];
            elseif data_segmented.trialinfo(i) == 204 %tools
                toolTrl = [toolTrl i];
            elseif data_segmented.trialinfo(i) == 205 %words
                wordTrl = [wordTrl i];
            else
                error('ERROR: Invalid category trigger code.');
            end
        end
    else
        if strcmp(condition, 'noCond') || strcmp(condition, 'lum_spat')
            for i = 1 : length(data_segmented.trialinfo)
                if data_segmented.trialinfo(i) == 21 %faces
                    faceTrl = [faceTrl i];
                elseif data_segmented.trialinfo(i) == 22 %scenes
                    sceneTrl = [sceneTrl i];
                elseif data_segmented.trialinfo(i) == 23 %bodies
                    bodyTrl = [bodyTrl i];
                elseif data_segmented.trialinfo(i) == 24 %tools
                    toolTrl = [toolTrl i];
                elseif data_segmented.trialinfo(i) == 25 %words
                    wordTrl = [wordTrl i];
                else
                    error('ERROR: Invalid category trigger code.');
                end
            end
        elseif strcmp(condition, 'lum')
            for i = 1 : length(data_segmented.trialinfo)
                if data_segmented.trialinfo(i) == 31 %faces
                    faceTrl = [faceTrl i];
                elseif data_segmented.trialinfo(i) == 32 %scenes
                    sceneTrl = [sceneTrl i];
                elseif data_segmented.trialinfo(i) == 33 %bodies
                    bodyTrl = [bodyTrl i];
                elseif data_segmented.trialinfo(i) == 34 %tools
                    toolTrl = [toolTrl i];
                elseif data_segmented.trialinfo(i) == 35 %words
                    wordTrl = [wordTrl i];
                else
                    error('ERROR: Invalid category trigger code.');
                end
            end
        else
            error('ERROR: Unknown condition identifier. Please specify ''lum'' or ''lum_spat''');
        end
    end

    display(sprintf('Number of face trials is %i', size(faceTrl,2)));
    display(sprintf('Number of scene trials is %i', size(sceneTrl,2)));
    display(sprintf('Number of body trials is %i', size(bodyTrl,2)));
    display(sprintf('Number of tool trials is %i', size(toolTrl,2)));
    display(sprintf('Number of word trials is %i', size(wordTrl,2)));
    display(sprintf('\nTotal number of valid trials is %i', size(faceTrl,2) + size(sceneTrl,2) + size(bodyTrl,2) + size(toolTrl,2) + size(wordTrl,2)));

    if (size(faceTrl,2) + size(sceneTrl,2) + size(bodyTrl,2) + size(toolTrl,2) + size(wordTrl,2) ~= length(data_segmented.trialinfo)) %error in division
        error('ERROR: Sum of all defined trials is not equal to the total number of trials.');
    end

    if (isempty(wordTrl)) && ((strcmp(class1,'word'))||(strcmp(class2,'word')))
        display('Word class analysis, but no words in this dataset. Skipping...');
    else
        %% Old classification script with old toolbox. Use new toolbox if works

        cfg             = [];
        cfg.parameter   = 'trial';
        cfg.keeptrials  = 'yes';
        cfg.channel     = 'MEG';

        eval(['cfg.trials = ', class1, 'Trl;']);
        cond1data       = ft_timelockanalysis(cfg, data_segmented);

        eval(['cfg.trials = ', class2, 'Trl;']);
        cond2data       = ft_timelockanalysis(cfg, data_segmented);

        % cross-validation
        cfg             = [];
        cfg.layout      = 'CTF275.lay';
        cfg.method      = 'crossvalidate';
        cfg.channel     = subject.MEGChannels; %default is all?
        %cfg.mva         = {ft_mv_standardizer ft_mv_svm};
        cfg.mva         = {ft_mv_standardizer ft_mv_glmnet('lambda',lambda)}; %old lambda 0.01, best lambda 0.0005
        cfg.design      = [ones(length(cond1data.trialinfo),1); 2*ones(length(cond2data.trialinfo),1)];
        eval(['tstat_', class1,'_', class2,' = ft_timelockstatistics(cfg,cond1data,cond2data);']);

        save([subject.subjectDir, '/', subject.subjID, '_', condition, '_sensorLevel_', class1,'-', class2 '_toi_', num2str(toilim(1)), '-', num2str(toilim(2)), '.mat'] ,'tstat*', 'cfg', '*Trl','-v7.3');
    end
end